#include <string>
#include <map>
#include "./server.hpp"

rudpServer :: stclient_tbl :: ~stclient_tbl()
{
	///@note 直接删除所有的连接是不正确的，必须把所有的连接全部确认断开后才可以
	/// 删除对应的内容
}
rudpServer :: stclient_tbl :: linkIt_t 
rudpServer :: stclient_tbl :: find( const struct sockaddr * add )
{
	linkIt_t ret = m_cli.end();

	hash_t   hash = hash_( (const char *)add , sizeof( struct sockaddr ));
	ret = m_cli.find( hash );

	return ret;
}
bool rudpServer :: stclient_tbl :: has( const struct sockaddr * add )
{
	hash_t   hash = hash_( (const char *)add , sizeof( struct sockaddr ));
	linkIt_t  it = m_cli.find( hash );

	return it != m_cli.end();
}

void rudpServer :: stclient_tbl :: insert( rudpLink * clink , const struct sockaddr * add )
{
	hash_t   hash = hash_( (const char *)add , sizeof( struct sockaddr ));

	if( has( add )){
		// TODO 添加如果HASH重复的处理代码
	}else{
		m_cli.insert( std::pair< hash_t , rudpLink *>( hash , clink ));
	}
}

void rudpServer :: stclient_tbl :: erase( const struct sockaddr * add )
{
	hash_t   hash = hash_( (const char *)add , sizeof( struct sockaddr ));

	linkIt_t  it = m_cli.find( hash );
	if( it != m_cli.end() )
		m_cli.erase( it );
}
void rudpServer :: stclient_tbl :: erase( hash_t t )
{
	linkIt_t  it = m_cli.find( t );
	if( it != m_cli.end() )
		m_cli.erase( it );
}

//////////////////////////////////////////////////////////////////////////////////////////////////
rudpServer :: rudpServer( int port )
: rudp( port )
{
}

rudpServer :: ~rudpServer()
{

}
void rudpServer :: do_on_connect( rudp_pkge_t * pkge , const struct sockaddr * add )
{
	if( pkge == nullptr ) return;
	
	rudp_pkge_t * pkg = nullptr;

	packet_connect_e( pkge , & pkg );
	if( pkg != nullptr )
		send_packet( pkg );
}

void rudpServer :: do_on_connect_echo( rudp_pkge_t * pkge , const struct sockaddr * peer )
{
	if( pkge == nullptr ) return;
	on_accept( peer );
}

void rudpServer :: dispatch( rudp_pkge_t * pkge , const struct sockaddr * add )
{
	
	stclient_tbl :: linkIt_t it = m_tbl.find( add );
}

void rudpServer :: close( hash_t t )
{

}

void rudpServer :: close( const struct sockaddr * add )
{

}

void rudpServer :: close_all()
{
	
}
